import grails.util.GrailsUtil

class JettyStaticTagLib {
	
	static namespace = "jettyStatic"
	
	def resource = {attrs ->
		def url = grailsApplication.config?.jettystatic.absolute.url
		def dir = grailsApplication.config?.jettystatic.basepath
		def file = attrs.file ?: ""
		def baseUrl = ""
		
		if(dir){
			if(!dir.endsWith('/')){
				dir = dir + '/'  
			}
			file = file.replaceFirst(dir,"")
		}
		if(url){
			baseUrl = url
		}else{			
			baseUrl = g.resource(dir:'resources')
		}
		
		def env = GrailsUtil.environment
		if(env && env == "development"){			
			baseUrl = g.resource(dir:'resources')
		}
		
		
		if(!baseUrl.endsWith('/')){
			baseUrl = baseUrl + '/'  
		}		
		out<< "${baseUrl}${file}"
	}
}